<!DOCTYPE html>
<html lang="en">
<head>
    <title>动态发布系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="UTF-8">
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="icon" href="image/icon/favicon.ico">
</head>
<body>
<div  class="container" style="width: 100%">
    <div class="row clearfix">
        <div class="col-sm-12 column">
            <ul class="nav nav-tabs">
                <li >
                    <a href="./index.html">首页</a>
                </li>
                <li >
                    <a href="./find.html">朋友圈</a>
                </li>
                <li class="active">
                    <a href="./message.html">消息</a>
                </li>
                <li >
                    <a href="./square.html" >广场</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="container" style="min-height: 325px;background-color: #F5F5F5">
    <div class="row clearfix" style="padding: 10px">
        <div class="col-sm-12 column" style="background-color: #F5F5F5">
            <table class="table">
                <thead>
                <tr>
                    <th>
                        用户
                    </th>
                    <th>
                        动态消息
                    </th>
                    <th>
                        时间
                    </th>
                </tr>
                </thead>
                <tbody id="table">

                </tbody>
            </table>
            <div>
                <ul id="page" class="pagination">
                </ul>
            </div>
        </div>
    </div>

</div>
</body>
<script>
    var page=0;
    var qian="/moments"
    var totalpage;
    $(function () {
        $.ajax({
            url:qian+"/loginInfo",
            headers:{
                'Authorization':localStorage.getItem('token')
            },
            async: false,
            success:function (data){
                if(data.code==200)
                {
                    var user=data.obj
                    if(user.nrmes!=0)
                    {
                        $("#nrmes").html("<span class=\"badge pull-right\">"+user.nrmes+"</span>消息")
                    }
                    gettotal();
                    getmessage(0)
                }
                else window.location.href="./login.html"
            },
            error:function (data){
                window.location.href="./login.html"
            }
        })
    })
    function gettotal(){
        $.ajax({
            url: qian+"/message/gettotal",
            headers: {
                'Authorization': localStorage.getItem('token')
            },
            async: false,
            success: function (data) {
                var total=data.obj;
                if(total==0)return
                totalpage=parseInt(total/5)+(total%5===0?0:1);
                var html="                    <li onclick='fanye("+(-1)+")'>\n" +
                    "                        <a href=\"#\">Prev</a>\n" +
                    "                    </li>"
                for(var i=1;i<=totalpage;i++)
                {
                    html+="                    <li onclick='fanye("+(i-1)+")'>\n" +
                        "                       <a href=\"#\"> "+i+"</a>\n" +
                        "                    </li>"
                }
                html+="                    <li onclick='fanye("+(-2)+")'>\n" +
                    "                        <a href=\"#\">Next</a>\n" +
                    "                    </li>"
                $('#page').html(html)
            }
        })
    }
    function fanye(val){
        if(val>=0){
            page=val;
        }
        else if(val===-1)
        {
            page--;
            page=Math.max(0,page);
        }
        else {
            page++;
            page=Math.min(page=Math.min(page,totalpage-1))
        }
        getmessage(page)
    }
    function getmessage(page){
        var user=JSON.parse(localStorage.getItem('user'))
        $.ajax({
            url:qian+"/message/getmes",
            headers:{
                'Authorization':localStorage.getItem('token')
            },
            data:{
                page:page
            },
            async: false,
            dataType: "json",
            success:function (data){
                if(data.code==200)
                {

                    var mes=data.obj;
                    var html=""
                    for(var i=0;i<mes.length;i++){
                        if(mes[i].type==0)
                        {
                            html+="                <tr onclick='tiao("+mes[i].type+","+mes[i].dyid+","+mes[i].manid+")' id=\"tr\""+i+" class=\""
                            html+= mes[i].isread? "success":"warning"
                            html+= "\">\n"
                        }
                        else if(mes[i].type==1)
                        {
                            html+="                <tr onclick='tiao("+mes[i].type+","+mes[i].dyid+","+mes[i].manid+")' id=\"tr\""+i+" class=\""
                            html+= mes[i].isread? "success":"warning"
                            html+= "\">\n"
                        }
                        html+= "                    <td>\n" +
                            "                        <img src=\""+mes[i].headphoto+"\" style=\"width: 50px;height: 50px;border-radius: 30px\">\n" +
                            "                    </td>\n" +
                            "                    <td>\n"+mes[i].mess+"" +
                            "                    </td>\n" +
                            "                    <td>\n"+mes[i].time+"" +
                            "                    </td>\n" +
                            "                </tr>"
                    }
                    $('#table').html(html)
                    claer();
                }
                else window.location.href="./login.html"
            }
        })
    }
    function claer(){
        $.ajax({
            url:qian+"/message/clearmes",
            headers:{
                'Authorization':localStorage.getItem('token')
            },
            type: "put",
            dataType: "json",
            success:function (data){
                if(data.code==200)
                {

                }
                else window.location.href="./login.html"
            }
        })
    }
    function tiao(type,dyid,manid){
        if(type==0)window.location.href="./details.html?dyid="+dyid;
        else window.location.href="./visitor.html?vid="+manid;
    }
</script>
</html>